# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=OpenSceneGraph

pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
_pkgver=3.2.1-rc3
pkgver=${_pkgver//-/}
pkgrel=3
pkgdesc="Open source high performance 3D graphics toolkit (mingw-w64)"
arch=('any')
url="http://www.openscenegraph.org/"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "${MINGW_PACKAGE_PREFIX}-cmake" "subversion")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
        "${MINGW_PACKAGE_PREFIX}-collada-dom-svn"
        "${MINGW_PACKAGE_PREFIX}-zlib"
        "${MINGW_PACKAGE_PREFIX}-boost"
        "${MINGW_PACKAGE_PREFIX}-curl"
        "${MINGW_PACKAGE_PREFIX}-lua"
        "${MINGW_PACKAGE_PREFIX}-freeglut"
        "${MINGW_PACKAGE_PREFIX}-libjpeg"
        "${MINGW_PACKAGE_PREFIX}-libpng"
        "${MINGW_PACKAGE_PREFIX}-libtiff"
        "${MINGW_PACKAGE_PREFIX}-libxml2"
        "${MINGW_PACKAGE_PREFIX}-SDL"
        "${MINGW_PACKAGE_PREFIX}-jasper"
        "${MINGW_PACKAGE_PREFIX}-qt5"
        "${MINGW_PACKAGE_PREFIX}-gtkglext"
        "${MINGW_PACKAGE_PREFIX}-giflib"
        "${MINGW_PACKAGE_PREFIX}-coin3d"
        "${MINGW_PACKAGE_PREFIX}-wxWidgets"
        )
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
options=('staticlibs' 'strip')
source=(http://www.openscenegraph.org/downloads/developer_releases/${_realname}-${_pkgver}.zip
        osg-collada-dae-fixes.patch
        find-collada.patch
        find-freetype.patch
        giflib.patch)
md5sums=('4ae56a16e5f03327792d156136730c34'
         '45d0668f56c5e4ca58002f38fd3eb4a0'
         '668def9c99f044db9a8215fa927d6ac2'
         '9ab69e1cd26826196fac5b3f01e6cb95'
         '849f641c915f17db85a49b355bf63a17')

prepare() {
  cd "${srcdir}/${_realname}-${_pkgver}"
  patch -p1 -i ${srcdir}/osg-collada-dae-fixes.patch
  patch -p1 -i ${srcdir}/find-collada.patch
  patch -p1 -i ${srcdir}/find-freetype.patch
  patch -p1 -i ${srcdir}/giflib.patch
}

build() {
  plain "No build step"
}

package_release() {
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  
  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null

  pushd ${MINGW_PREFIX} > /dev/null
  export PREFIX_DEPS=`pwd -W`
  popd > /dev/null
  
  [[ -d ${srcdir}/build-release ]] && rm -rf ${srcdir}/build-release
  mkdir -p ${srcdir}/build-release && cd ${srcdir}/build-release
  export FREETYPE_DIR=${PREFIX_DEPS}
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${pkgdir}${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_OSG_EXAMPLES:BOOL=ON \
    -DACTUAL_3DPARTY_DIR=${MINGW_PREFIX} \
    ../${_realname}-${_pkgver}
  
  make
  make -j1 install
  
  for ff in ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/*.pc; do
    sed -i "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" $ff
  done
}

package_debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=$pkgver")
  options=('staticlibs' '!strip')
  pkgdesc="Open source high performance 3D graphics toolkit (debug) (mingw-w64)"
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
  
  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null

  pushd ${MINGW_PREFIX} > /dev/null
  export PREFIX_DEPS=`pwd -W`
  popd > /dev/null
  
  [[ -d ${srcdir}/build-debug ]] && rm -rf ${srcdir}/build-debug
  mkdir -p ${srcdir}/build-debug && cd ${srcdir}/build-debug
  export FREETYPE_DIR=${PREFIX_DEPS}
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${pkgdir}${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Debug \
    -DBUILD_OSG_EXAMPLES:BOOL=OFF \
    -DBUILD_OSG_APPLICATIONS=OFF \
    -DACTUAL_3DPARTY_DIR=${MINGW_PREFIX} \
    ../${_realname}-${_pkgver}
  
  make
  make -j1 install

  rm -rf ${pkgdir}${MINGW_PREFIX}/include
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig
  rm -rf ${pkgdir}${MINGW_PREFIX}/share
}

package_mingw-w64-i686-OpenSceneGraph() {
  package_release
}

package_mingw-w64-i686-OpenSceneGraph-debug() {
  package_debug
}

package_mingw-w64-x86_64-OpenSceneGraph() {
  package_release
}

package_mingw-w64-x86_64-OpenSceneGraph-debug() {
  package_debug
}
